package com.elves.auth.api;

import cn.hutool.json.JSONObject;
import com.elves.auth.service.UserService;
import com.elves.auth.util.RSAUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class AuthAction {
    @Resource
    UserService userService;

    @RequestMapping("/getPublicKey")
    public String getPublicKey() throws Exception {
        log.info("getPublicKey:{}", userService.getPublicKey());
        return userService.getPublicKey();
    }

    @PostMapping(value = "/loginRequest")
    @ResponseBody
    public String loginRequest(@RequestBody JSONObject json) {
        String username = json.getStr("username");
        String password = json.getStr("password");
        log.info("username:{}",username);
        log.info("password:{}",password);
        String res = null;
        try {
            res = RSAUtil.decryptByPrivateKey(userService.getPublicKey(), password);
            //这里就是解密后的密码了
            log.info("res:{}",res);
        } catch (Exception e) {
            //  e.printStackTrace();
            log.info("ERROR:{}",e.toString());
        }
        return res;
    }

}
